<?php
/**
 * Created by PhpStorm.
 * User: dingboxu
 * Date: 16/3/11
 * Time: 下午4:30
 */

namespace Actions;

use GatewayWorker\Lib\Gateway;

class BaseAction
{
    public $dataArr = array();

    public function __construct($event, $requestId, $data, $clientId)
    {
        $this->data = $data;
        $this->clientId = $clientId;
        $this->event = $event;
        $this->requestId = '0';
        if($requestId)
        {
            $this->requestId = $requestId;
        }
        // 响应码
        $this->resultCode = RESULT_CODE_OK;
        // 错误信息
        $this->errMsg = "";

        $this->doAction();
    }

    public function doAction()
    {

    }

    public function setValue($key,$value)
    {
        $this->dataArr[$key] = $value;
    }

    /**
     *
     * 处理成功，返回通讯
     *
     */
    public function ResponseDataToCurrentClient() {
        Gateway::sendToCurrentClient(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_OK,$this->errMsg));
    }


    /**
     * @param $errMsg
     * @return null
     *
     * 参数异常
     *
     */
    public function SendErrMsg($errMsg) {
        Gateway::sendToCurrentClient(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_PARAM_ERROR,$errMsg));
        return null;
    }


    /**
     * @param $errMsg
     * @return null
     *
     * 会话已过期
     *
     */
    public function SendSessionOut($errMsg) {
        Gateway::sendToCurrentClient(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_SESSION_EXPIRED,$errMsg));
        return null;
    }

    /**
     * @param $errMsg
     * @return null
     *
     * 非法操作
     *
     */
    public function SendIllegalOperation($errMsg) {
        Gateway::sendToCurrentClient(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_ILLEGAL_OPERATION,$errMsg));
        return null;
    }


    /**
     * @param $errMsg
     * @return null
     *
     * 找不到该通讯
     *
     */
    public function SendNotFound($errMsg) {
        Gateway::sendToCurrentClient(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_NOT_FOUND,$errMsg));
        return null;
    }


    /**
     * @param $errMsg
     * @return null
     *
     * 服务器内部错误
     *
     */
    public function SendInternalServerError($errMsg) {
        Gateway::sendToCurrentClient(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_INTERNAL_SERVER_ERROR,$errMsg));
        return null;
    }

}